package com.shaoshaohuo.app.view;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.util.List;

/**
 * Created by Administrator on 2017/6/8.
 */

public class VerticalViewPager extends ViewPager {
    private VerticalViewPager verticalvP;
    private List<Fragment> list;
    private float starty;

    public VerticalViewPager(Context context) {
        this(context, null);
    }

    public VerticalViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        setPageTransformer(false, new DefaultTransformer());
    }

    private MotionEvent swapTouchEvent(MotionEvent event) {
        float width = getWidth();
        float height = getHeight();

        float swappedX = (event.getY() / height) * width;
        float swappedY = (event.getX() / width) * height;

        event.setLocation(swappedX, swappedY);

        return event;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        boolean intercept = super.onInterceptTouchEvent(swapTouchEvent(event));
        //If not intercept, touch event should not be swapped.
        swapTouchEvent(event);
        return intercept;
    }

    private boolean aBoolean = true;

    public void setaBoolean(boolean v) {
        if (!v) {
            LinearLayout childAt1 = (LinearLayout) this.getChildAt(1);
            ViewPager childAt2 = (ViewPager) childAt1.getChildAt(0);
            View childAt = childAt2.getChildAt(0);
            int scrollY = childAt.getScrollY();
            if (scrollY != 0) {
                return;
            }
        }
        this.aBoolean = v;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (aBoolean) {

            return true;
        } else {
            return super.onTouchEvent(swapTouchEvent(ev));
        }

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                starty = ev.getY();
                this.setaBoolean(true);
                break;
            case MotionEvent.ACTION_MOVE:
                float y = ev.getY();
                float v = starty - y;
                if (this.getCurrentItem() == 1 && aBoolean && ((ViewPager) ((LinearLayout) this.getChildAt(1)).getChildAt(0)).getCurrentItem() == 0) {
                    LinearLayout childAt1 = (LinearLayout) this.getChildAt(1);
                    ViewPager childAt2 = (ViewPager) childAt1.getChildAt(0);
                    View childAt = childAt2.getChildAt(0);
                    int scrollY = childAt.getScrollY();
                    float v1 = v + scrollY;
                    childAt.scrollTo(0, (int) (v1));
                    Log.e("TAG", "dispatchTouchEvent: " + v + "childAt.getScrollY()" + childAt.getScrollY());
                    if (childAt.getScrollY() != 0) {
                        this.setaBoolean(true);
                    } else if (childAt.getScrollY() == 0) {
                        if (v < 0) {
                            this.setaBoolean(false);
                        }
                    }
                }
                if(this.getCurrentItem() == 0&&aBoolean ){
                    RelativeLayout childAt1 = (RelativeLayout) this.getChildAt(0);
                    if(childAt1!=null){
                        MySrcollView childAt = (MySrcollView) childAt1.getChildAt(0);
                        int scrollY = childAt.getScrollY();
                        float v1 = v + scrollY;
                        childAt.scrollTo(0, (int) (v1));
                        if (childAt.getChildAt(0).getHeight() != childAt.getScrollY() + (childAt.getHeight() - getScrollY())) {
//                        Log.e("TAG", "dispatchTouchEvent: " + childAt.getChildAt(0).getHeight() + "childAt.getScrollY()===" + (childAt.getScrollY() + (childAt.getHeight() - getScrollY())));
                            this.setaBoolean(true);
                        } else {
                            this.setaBoolean(false);
                        }
                    }

            }
                starty = y;
                break;
            case MotionEvent.ACTION_UP:
                break;
        }

        return super.dispatchTouchEvent(ev);
    }


}
